package application.controller;

import application.adapter.TableAdapter;
import application.anno.Column;
import application.anno.Layout;
import application.anno.StageTitle;
import application.windows.UserInterface;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;

import java.util.ArrayList;
import java.util.List;

@Layout("TableShow.fxml")
@StageTitle("表格视图展示")
public class TableShow extends UserInterface {

    @FXML
    private TableView<ListDemo.Entry> tableShow;

    private TestAdapter adapter;

    @FXML
    private Button backup;

    @Column(columns = {"编号", "键", "值", "附加", "说明"})
    public static class TestAdapter extends TableAdapter {

        public TestAdapter(TableView tableView) {
            super(tableView);
        }

    }

    @Override
    @FXML
    protected void initialize() throws IllegalAccessException {
        super.initialize();
        backup.setOnMouseClicked(event -> {
            end();
        });

        adapter = new TestAdapter(tableShow);
        adapter.bindWidthToTableView(ListDemo.Entry.class);
        List<ListDemo.Entry> entries = new ArrayList<>();
        ListDemo.Entry entry = new ListDemo.Entry();
        entry.setId(1);
        entry.setKey("key1");
        entry.setValue("value1");
        entry.setExtra("extra1");
        entry.setRemark("remark1");
        entries.add(entry);

        entry = new ListDemo.Entry();
        entry.setId(2);
        entry.setKey("key2");
        entry.setValue("value2");
        entry.setExtra("extra2");
        entry.setRemark("remark2");
        entries.add(entry);

        entry = new ListDemo.Entry();
        entry.setId(3);
        entry.setKey("key3");
        entry.setValue("value3");
        entry.setExtra("extra3");
        entry.setRemark("remark3");
        entries.add(entry);

        entry = new ListDemo.Entry();
        entry.setId(4);
        entry.setKey("key4");
        entry.setValue("value4");
        entry.setExtra("extra4");
        entry.setRemark("remark4");
        entries.add(entry);

        entry = new ListDemo.Entry();
        entry.setId(5);
        entry.setKey("key5");
        entry.setValue("value5");
        entry.setExtra("extra5");
        entry.setRemark("remark5");
        entries.add(entry);
        adapter.setItems(entries);
    }
}
